package service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.Echarts_dao;
import dto.echarts.Option;
import dto.echarts.Serie;
import enums.travelenums;
import service.Echarts_service;
import util.DateUtil;

@Service
public class Echarts_service_impl implements Echarts_service {

	@Autowired
	private Echarts_dao echarts_dao;
	
	@Override
	public Option countOrder() {
		Option option=new Option();
		List<Map<String, String>> list=echarts_dao.countOrder();
		HashSet<String> oderTypeNameSet=new HashSet<>();
		oderTypeNameSet.add(travelenums.CANCEL.getInfo());
		oderTypeNameSet.add(travelenums.TOPAY.getInfo());
		oderTypeNameSet.add(travelenums.SUCCPAY.getInfo());
		oderTypeNameSet.add(travelenums.COMMENTED.getInfo());
		Map<String, Long> dataMap=new HashMap<>();
		//装配类别数据,装配一个以（类别+日期）为键，以（数量）为值的Map
		for (Map<String, String> map : list) {
			//oderTypeNameSet.add(travelenums.stateOf(Integer.parseInt(map.get("status"))).getInfo());
			dataMap.put(travelenums.stateOf(Integer.parseInt(map.get("status"))).getInfo()+map.get("date"), Long.valueOf(map.get("count")));
		}
		option.getLegend().setData(new ArrayList<>(oderTypeNameSet));
		//装配X轴数据
		List<String> dateList=DateUtil.get_dateList();
		option.getxAxis().setData(dateList);//日期工具类获取前一个月的全部日期
		//装配某类别某天的那条线上的数据
		for(String oderTypeName:option.getLegend().getData()){
			Serie serie=new Serie();
			serie.setName(oderTypeName);
			serie.setType("line");
			for(String date : dateList) {
				serie.getData().add(dataMap.get(oderTypeName+date)==null ? 0 : dataMap.get(oderTypeName+date));
			}
			option.getSeries().add(serie);
		}
		return option;
	}

}
